package zhongbo.java.air.flightpath.web.controller;

import cn.hutool.core.date.DateTime;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import zhongbo.java.air.common.vo.PageVo;
import zhongbo.java.air.common.vo.ResultVo;
import zhongbo.java.air.flightpath.model.FlightPath;
import zhongbo.java.air.flightpath.model.enums.SourceType;
import zhongbo.java.air.flightpath.service.IFlightPathService;

import java.util.Date;

@RestController
@RequestMapping("/flightpath")
@Api(tags = "航迹基础信息操作的相关接口")
public class FlightPathController {
    @Autowired
    IFlightPathService flightPathService;
    @PostMapping("add")
    @ApiOperation("新增航迹")
    ResultVo<FlightPath> add(FlightPath flightPath){
        if (flightPath == null || flightPath.equals("")){
            return ResultVo.error("传入数据为空");
        }
        if (!flightPath.getIcaoCode().matches("^[0-9]{1,6}$")){
            return ResultVo.wrong(404,"ICAO地址码只能是1~6位的数字");
        }
        if (flightPath.getHeading()>359 || flightPath.getHeading()<0){
            return ResultVo.wrong(404,"航向角范围(0-359)");
        }
        if (flightPath.getAltitude()<0){
            return ResultVo.wrong(404,"海拔高度不能为负数");
        }
        PageVo page = flightPathService.page(flightPath.getIcaoCode(),1,1);
        if (page.getRecords().size() == 0) {
            flightPath.setTimestamp(DateTime.now());
            flightPath.setSourceType(String.valueOf(SourceType.ADS_B));
            int i = flightPathService.addFlight(flightPath);
            if (i <= 0) {
                return ResultVo.wrong(404, "添加失败");
            }
            return ResultVo.ok(flightPath);
        }
        return ResultVo.error("有相同ICAO数据");
    }

    @GetMapping("delete")
    @ApiOperation("删除信息")
    ResultVo<FlightPath> delete(String id){
        if (id == null || id.equals("")){
            return ResultVo.error("id为空");
        }
        FlightPath flightPath = flightPathService.selectById(id);
        flightPathService.deleteFlight(id);
        return ResultVo.ok(flightPath);
    }
    @PostMapping("update")
    @ApiOperation("更新信息")
    ResultVo<FlightPath> update(FlightPath flightPath){
        if (flightPath == null || flightPath.equals("")){
            return ResultVo.error("数据信息为空");
        }
        flightPath.setSpeed("240km/h");
        int updateFlight = flightPathService.updateFlight(flightPath);
        if (updateFlight <= 0){
            return ResultVo.error("更新失败");
        }
        return ResultVo.success();
    }

    @GetMapping("datil")
    @ApiOperation("航迹详细详细")
    ResultVo<FlightPath> selectById(String id){
        if (id == null || id.equals("")){
            return ResultVo.error("id为空");
        }
        FlightPath flightPath = flightPathService.selectById(id);
        if (flightPath == null || flightPath.equals("")){
            return ResultVo.error("查询失败");
        }
        return ResultVo.ok(flightPath);
    }
    @GetMapping("page")
    @ApiOperation("ICAO分页查询")
    ResultVo<PageVo<FlightPath>> page(String icaoCode, Integer pageNumber, Integer pageSize){
        PageVo<FlightPath> pageVo = flightPathService.page(icaoCode,pageNumber,pageSize);
        return ResultVo.ok(pageVo);
    }

    @GetMapping("pageTime")
    @ApiOperation("Time分页查询")
    ResultVo<PageVo<FlightPath>> pageTime(String startTime, String endTime, Integer pageNumber, Integer pageSize){
        PageVo<FlightPath> pageVo = flightPathService.pageTime(startTime,endTime,pageNumber,pageSize);
        return ResultVo.ok(pageVo);
    }

}
